<?php
require_once('db_utils.php');

/*
 * Store PHP Session data in mysql database instead of the default php file.
 *
 * Original source: http://jamesholden.net/php-code/php-mysql-database-session-handler/
 *
 * Modified to use custom DbUtils class.
 */
class SessionToDb
{
	var $db_manager;
	var $dbhSession;
	var $uxtLifetime;
	
	function connect()
	{
		if(!$this->dbhSession)
		{
			$this->db_manager = new DbUtils();
			$this->db_manager->connect();
			$this->dbhSession = &$this->db_manager->db_con;
		}
	}

	function open($strSavePath, $strName)
	{
		// overwriting session life time to 60 seconds and ignoring the php default
		$this->uxtLifetime = 60;
		$this->connect();
		
		return true;
	}

	function close()
	{
		$this->connect();
		$this->garbageCollect( ini_get('session.gc_maxlifetime') );
		$close_result = $this->db_manager->close();
		$this->db_manager = null;
		$this->dbhSession = false;
		
		return $close_result;
	}
	
	function read($strName)
	{
		$retval;
		$this->connect();
		
		$strSql = "SELECT txtData FROM tblSession WHERE vchName = '$strName' AND uxtExpires > " . time();
		$refResult = $this->db_manager->query($strSql);
		
		if($arrData = mysql_fetch_assoc($refResult))
		{
			$retval = $arrData['txtData'];
		}
		else
		{
			$retval = '';
		}
		
		return $retval;
	}

	function write($strName, $unkData)
	{
		$this->connect();
		
		$unkData = addslashes($unkData); // Do we need this?
		$uxtExpire = time() + $this->uxtLifetime;
		$strSql = "SELECT * FROM tblSession WHERE vchName = '$strName'";
		$refResult = $this->db_manager->query($strSql);
		
		if(mysql_num_rows($refResult))
		{
			$strSql = "UPDATE tblSession SET uxtExpires = '$uxtExpire', txtData = '$unkData' WHERE vchName = '$strName'";
			$refResult = $this->db_manager->query($strSql);
		}
		else
		{
			$strSql = "INSERT INTO tblSession (vchName, uxtExpires, txtData) VALUES ('$strName', '$uxtExpire', '$unkData')";
			$refResult = $this->db_manager->query($strSql);
		}
		
		return true;
	}

	function destroy($strName)
	{
		$this->connect();
		$strSql = "DELETE FROM tblSession WHERE vchName = '$strName'";
		$refResult = $this->db_manager->query($strSql);
	}

	function garbageCollect($uxtSessionMaxLifetime)
	{
		$this->connect();
		$strSql = "DELETE FROM tblSession WHERE uxtExpires < '" . time() . "'";
		$refResult = $this->db_manager->query($strSql);
	}

	function setLifetime($intSeconds)
	{
		$this->uxtLifetime = $intSeconds;
	}
}

// We only want sessions for web things
if(isset($_SERVER['DOCUMENT_ROOT']))
{
	$db_session_handler = new SessionToDb();
	
	session_set_save_handler(array(&$db_session_handler,"open"),
	                         array(&$db_session_handler,"close"),
	                         array(&$db_session_handler,"read"),
	                         array(&$db_session_handler,"write"),
	                         array(&$db_session_handler,"destroy"),
	                         array(&$db_session_handler,"garbageCollect"));
}
?>
